package com.rw.coco.listener;

import com.rw.coco.api.bot.CocoApi;
import com.rw.coco.event.KookPrivateMessageEvent;
import snw.jkook.event.EventHandler;
import snw.jkook.event.Listener;
import snw.jkook.event.pm.PrivateMessageReceivedEvent;
import snw.jkook.message.PrivateMessage;
import snw.jkook.message.component.BaseComponent;
import snw.jkook.message.component.MarkdownComponent;

/**
 * 私聊消息监听器
 */
public class CocoPrivateListener implements Listener {
    private final CocoApi coco;

    public CocoPrivateListener(CocoApi coco) {
        this.coco = coco;
    }

    @EventHandler
    public void onEvent(PrivateMessageReceivedEvent event) {
        // 不接收机器人信息
        if (event.getUser().isBot()) {
            return;
        }

        // 获取消息
        PrivateMessage eventMessage = event.getMessage();
        BaseComponent component = eventMessage.getComponent();

        // 如果是文字消息
        if (component instanceof MarkdownComponent) {
            KookPrivateMessageEvent messageEvent = new KookPrivateMessageEvent(eventMessage);
            coco.getMessageHandler().handle(messageEvent);
        }
    }
}
